
#include "VertexBuffer.h"




VertexBuffer::VertexBuffer(VertexBufferDescr& vbDescr)
{
	// copy vertex buffer description
	_vbDescr = vbDescr;
	_vbDescr.vertexDescrPtr = new VertexAttribDescr[_vbDescr.vertexAttribsCount];
	for (int i = 0; i < _vbDescr.vertexAttribsCount; i++) {
		_vbDescr.vertexDescrPtr[i] = vbDescr.vertexDescrPtr[i];
	}

	// setup buffers
	_buffer = new char*[vbDescr.vertexAttribsCount];
	_attribsCount = vbDescr.vertexAttribsCount;
	_vertexCount = vbDescr.vertexCount;
	_vertexUsed = 0;

	for (int i = 0; i < _attribsCount; i++)
		_buffer[i] = nullptr;
}

void VertexBuffer::setBufferAttribData(GLuint attribId, void* data, GLuint count)
{
	int bufferIndex = UNDEF;

	for (int i = 0; i < _vbDescr.vertexAttribsCount; i++) {
		if (_vbDescr.vertexDescrPtr[i].attribId == attribId) {
			bufferIndex = i;
		}
	}

	assert(bufferIndex != UNDEF);

	_buffer[bufferIndex] = (char*)data;
}

char* VertexBuffer::getDataWithAttribId(int attribId)
{
	int bufferIndex = UNDEF;

	for (int i = 0; i < _vbDescr.vertexAttribsCount; i++) {
		if (_vbDescr.vertexDescrPtr[i].attribId == attribId) {
			bufferIndex = i;
		}
	}

	assert(bufferIndex != UNDEF || _buffer[bufferIndex] == nullptr);

	return _buffer[bufferIndex];
}